package com.firefly.curve.attr;

import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;

public class StringRec {
	private Graphics g;
	private int width;
	private int height;
	private double lineHeight;
	private String val;
	private String[] strs;
	
	public StringRec(Graphics g){
		this.g=g;
	}
	
	public void drawString(int x,int y){
		for(int i=0;i<strs.length;i++){
			g.drawString(strs[i], x, y+(int)(lineHeight*i));
		}
	}
	
	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public String getVal() {
		return val;
	}
	public void setVal(String val) {
		this.val = val;
        
        strs=val.split("\n");
		//获取打印字符的宽和高
		FontMetrics fm2 = g.getFontMetrics();  
		Rectangle2D rec2=null;
		
		width=0;
		height=0;
		lineHeight=0;
		for(String s:strs){
			rec2=fm2.getStringBounds(s, g);
			lineHeight=rec2.getHeight();
			width=rec2.getWidth()>width?(int)rec2.getWidth():width;
			height+=(int)rec2.getHeight();
		}
	}
	
	public double getLineHeight() {
		return lineHeight;
	}
}
